#include <omp.h>
#include <iostream>
#include <ctime>
#include <chrono>

using namespace std;
// int main()
// {
//     int a[100000], b[100000];
//     int c[100000];
//     clock_t t1, t2;
//     double cost_time1, cost_time2;
//     //a = (int *)malloc(100000);
//     //b = (int *)malloc(100000);
//     // ... some initialization code for populating arrays a and b;
//     for (int i = 0; i < 100000; ++i)
//     {
//         a[i] = i;
//         b[i] = i;
//     }

//     //c = (int *)malloc(100000);
//     omp_set_num_threads(4);

//     t1 = clock();
// #pragma omp parallel for
//     for (int j = 0; j < 4; j++)
//     {

//         for (int i = 0; i < 100000; ++i)
//         {
//             c[i] = i;
//         }
//     }

//     // ... now do some processing with array c
//     cost_time1 = (double)(clock() - t1) / CLOCKS_PER_SEC * 1000;
//     std::cout << "cost time1:" << cost_time1 << "ms" << std::endl;
// }

void test()
{
    int a = 0;
    //clock_t t1 = clock();

    chrono::steady_clock::time_point t1 = chrono::steady_clock::now();

    for (int i = 0; i < 1000000000; i++)
    {
        a = i + 1;
    }
    chrono::steady_clock::time_point t2 = chrono::steady_clock::now();
    chrono::duration<double> time_used =
        chrono::duration_cast<chrono::duration<double>>(t2 - t1);
    cout << "时间：" << time_used.count() * 1000 << "ms" << endl;

    //clock_t t2 = clock();
    //printf("Time = %d\n", t2 - t1);
}
int main(int argc, char *argv[])
{
    omp_set_num_threads(4);
    chrono::steady_clock::time_point t1 = chrono::steady_clock::now();
#pragma omp parallel for
    for (int j = 0; j < 4; j++)
    {
        test();
        printf("Hello, World!, ThreadId=%d\n", omp_get_thread_num());
    }
    chrono::steady_clock::time_point t2 = chrono::steady_clock::now();
    chrono::duration<double> time_used =
        chrono::duration_cast<chrono::duration<double>>(t2 - t1);
    cout << "总时间：" << time_used.count() * 1000 << "ms" << endl;

    t1 = chrono::steady_clock::now();
    ;
    for (int j = 0; j < 4; j++)
    {
        test();
        printf("Hello, World!, ThreadId=%d\n", omp_get_thread_num());
    }
    t2 = chrono::steady_clock::now();
    time_used =
        chrono::duration_cast<chrono::duration<double>>(t2 - t1);
    cout << "总时间：" << time_used.count() * 1000 << "ms" << endl;

    return 0;
}

// int main(int argc, char *argv[]) {  
// #pragma omp parallel num_threads(8)  
// {  
//     printf("Hello, World!, ThreadId=%d\n", omp_get_thread_num() );  
// }  
// }  
